import {defineStore} from "pinia";
import {createStorage, storage} from "@/utils/Storage";

import {
  SHEET_QUERY_PARAMS,
  COLUMN_QUERY_PARAMS,
  TREND_QUERY_PARAMS,
} from '@/store/mutation-types'
import {queryNearlyDays, queryProjectCounts, queryStatistics} from "@/api/statistics/stastistics";
import {nearlyDays} from "@/utils/dateUtil";

const Storage = createStorage({storage: localStorage});
const ex = 7 * 24 * 3600 * 1000;

export type ISheetParams = {
  pageSize: number | null;
  project: number | null;
  region: number | null;
  dateType: string;
  dateTypeLabel: any;
  begin: string | null;
  end: string | null;
}
export type IChartParams = {
  project: number | null;
  days: number;
  begin: string | null;
  end: string | null;
}

export type IQueryParams = {
  sheet_chart: ISheetParams;
  column_chart: IChartParams;
  trend_chart: IChartParams
}

export const QueryParamsStore = defineStore({
  id: 'query-params',
  state: (): IQueryParams => ({
    sheet_chart: Storage.get(SHEET_QUERY_PARAMS, {
      pageSize: 10,
      project: null,
      dateType: 'daterange',
      dateTypeLabel: '日期',
      region: null,
      begin: '',
      end: ''
    }),
    column_chart: Storage.get(COLUMN_QUERY_PARAMS, {
      project: null,
      days: 7,
      begin: nearlyDays(7),
      end: nearlyDays(1)
    }),
    trend_chart: Storage.get(TREND_QUERY_PARAMS, {
      project: null,
      days: 7,
      begin: nearlyDays(7),
      end: nearlyDays(1)
    })
  }),
  getters: {
    getSheetParams(): ISheetParams {
      return this.sheet_chart;
    },
    getColumnParams(): IChartParams {
      return this.column_chart;
    },
    getTrendParams(): IChartParams {
      return this.trend_chart;
    }
  },
  actions: {
    setSheetChartParams(params) {
      this.sheet_chart = params;
      storage.set(SHEET_QUERY_PARAMS, params, ex);
    },
    setColumnChatParams(params) {
      this.column_chart = params;
      storage.set(COLUMN_QUERY_PARAMS, params, ex);
    },
    setTrendChartParams(params) {
      this.trend_chart = params;
      storage.set(TREND_QUERY_PARAMS, params, ex);
    },
    async queryProjectCounts(queryParams) {
      try {
        this.setTrendChartParams(queryParams);
        const response = await queryProjectCounts(queryParams);
        return Promise.resolve(response);
      } catch (e) {
        return Promise.reject(e);
      }
    },
    async queryNearlyDays(queryParams) {
      try {
        this.setColumnChatParams(queryParams);
        const response = await queryNearlyDays(queryParams);
        return Promise.resolve(response);
      } catch (e) {
        return Promise.reject(e);
      }
    },
    async queryStatistics(queryParams) {
      try {
        this.setSheetChartParams(queryParams);
        const response = await queryStatistics(queryParams);
        return Promise.resolve(response);
      } catch (e) {
        return Promise.reject(e);
      }
    },
    clearSheetData() {
      storage.remove(SHEET_QUERY_PARAMS);
      this.sheet_chart = {
        pageSize: 10,
        project: null,
        dateType: 'daterange',
        dateTypeLabel: '日期',
        region: null,
        begin: '',
        end: ''
      };
    }
  },
});
